<template>
  <el-dialog title="采购退货申请" :visible.sync='refundGoodsVisible' width="580px">
    <div v-loading="loading" element-loading-text="加载中">
      <el-form ref="purchaseOrderForm" :model="purchaseOrderForm" :rules="rules" label-width="150px"
        style="width: 530px">
        <el-form-item label="关联采购申请单号：" v-if="purchaseOrderForm.relationPurchaseOrderNo">
          <el-input v-model="purchaseOrderForm.relationPurchaseOrderNo" style="width: 300px" :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item label="供应商：">
          <el-select v-model="purchaseOrderForm.supplierId" style="width: 300px" :disabled="supplierEditFlag"
            placeholder="不限">
            <el-option label="不限" value=""></el-option>
            <el-option v-for="(item, index) in supplierList" :key="index" :label="item.supplierName"
              :value="item.supplierId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注：">
          <el-input type="textarea" v-model="purchaseOrderForm.remark" :rows="3" class="width-300"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="doSave">保存</el-button>
        <el-button @click="doCancel">取消</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
  import {
    listSupplier
  } from '@/api/user/supplier'
  import {
    getPurchaseOrder,
    savePurchaseOrder
  } from '@/api/warehouse/purchaseOrder'
  export default {
    props: {
      onSuccessCall: Function
    },
    data() {
      return {
        refundGoodsVisible: false,
        // 采购订单表单
        purchaseOrderForm: {},
        loading: false,
        dialogTitle: '',
        // 供应商编辑Flag
        supplierEditFlag: false,
        // 供应商集合
        supplierList: [],
        // 省集合
        provinceList: [],
        // 市集合
        cityList: [],
        // 区集合
        districtList: [],
        rules: {}
      }
    },
    watch: {
      // showFlag(val) {
      //   if (val) {
      //     Object.assign(this.purchaseOrderForm, this.purchaseOrder)
      //     this.loading = true
      //     this.init()
      //   }
      // }
    },
    created() {
      const supplierForm = {
        enterpriseId: this.getEnterpriseId()
        // settlementType: 30
      }
      // 获取供应商列表
      listSupplier(supplierForm).then(response => {
        if (response.isError) return
        this.supplierList = response.data
      })
      // 获取仓库信息
      // getWarehouse(this.getWarehouseId()).then((response) => {
      //   this.warehouseInfoObj = response.data
      //   this.init()
      // })
    },
    methods: {
      // 初始化
      init() {
        this.loading = true
        if (this.purchaseOrderForm.purchaseOrderId) {
          getPurchaseOrder(this.purchaseOrderForm.purchaseOrderId).then(response => {
            if (response.data.relationPurchaseOrderNo) {
              // 优先判断是否禁止选择供应商
              this.supplierEditFlag = true
            } else {
              this.supplierEditFlag = response.data.purchaseOrderGoodsList.length > 0
            }
            this.purchaseOrderForm.remark = response.data.remark
            this.purchaseOrderForm.supplierId = response.data.supplierId
            this.purchaseOrderForm.relationPurchaseOrderNo = response.data.relationPurchaseOrderNo
            var warehouseGoodsIds = []
            response.data.purchaseOrderGoodsList.forEach(goodsItem => warehouseGoodsIds.push(
              goodsItem.warehouseGoodsId))
            this.purchaseOrderForm.warehouseGoodsIds = warehouseGoodsIds
            this.loading = false
          })
        } else {
          // 新增的时候，如果有来源的话，不能修改供应商
          if (this.purchaseOrderForm.relationPurchaseOrderNo) {
            this.supplierEditFlag = true
            this.loading = true
            getPurchaseOrder(this.purchaseOrderForm.relationPurchaseOrderId).then(response => {
              this.loading = false
              if (response.isError) {
                return
              }
              var warehouseGoodsIds = []
              response.data.purchaseOrderGoodsList.forEach(goodsItem => warehouseGoodsIds.push(
                goodsItem.warehouseGoodsId))
              this.purchaseOrderForm.warehouseGoodsIds = warehouseGoodsIds
            })
          }
          this.loading = false
        }
      },
      // 初始化表单
      initForm() {
        this.purchaseOrderForm = {
          enterpriseId: this.getEnterpriseId(),
          warehouseId: this.getWarehouseId(),
          purchaseOrderId: '',
          supplierId: '',
          remark: '',
          warehouseGoodsIds: [],
          relationPurchaseOrderNo: '',
          relationPurchaseOrderId: ''
        }
      },
      // 保存事件
      doSave() {
        this.$refs['purchaseOrderForm'].validate((valid) => {
          if (valid) {
            if (!this.purchaseOrderForm.purchaseOrderId) {
              this.purchaseOrderForm.enterpriseId = this.getEnterpriseId()
              this.purchaseOrderForm.warehouseId = this.getWarehouseId()
            }
            this.purchaseOrderForm.purchaseOrderType = 2
            this.loading = true
            savePurchaseOrder(this.purchaseOrderForm).then(response => {
              this.loading = false
              if (response.isError) return
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              if (!this.purchaseOrderForm.purchaseOrderId) {
                this.$router.push({
                  path: '/warehouse/warehouse-refund-goods/detail/index',
                  query: {
                    purchaseOrderId: response.data
                  }
                })
              } else {
                this.onSuccessCall()
              }
              this.doCancel()
            })
          }
        })
      },
      // 取消事件
      doCancel() {
        this.refundGoodsVisible = false
      },
      open(purchaseOrder) {
        this.refundGoodsVisible = true
        this.initForm()
        Object.assign(this.purchaseOrderForm, purchaseOrder)
        this.init()
      }
    }
  }
</script>

<style lang="less" scoped>

</style>
